<?php

/**
 *    商品规格管理控制器
 *
 *    @author    censhuixing
 */
class SpecApp extends BackendApp
{
	var $_spec_mod;
	var $spec_input_type = array(
	0 => '手工录入',
	//1 => '单选属性',
	2 => '复选属性',
	//3 => '列表属性',
	//4 => '多行文本框',
	);

	function __construct()
	{
		$this->SpecApp();
	}

	function SpecApp()
	{


		parent::BackendApp();


		$this->_spec_mod =& m('spec');
	}

	/**
     *    商品品牌索引
     *
     *    @author    Hyber
     *    @return    void
     */
	function index()
	{
		$cid = intval($_GET['cid']);
		$specs = $this->_spec_mod->get_list($cid);
		if(is_array($specs))
		{
			foreach ($specs as $key=>$val)
			{			
				$specs[$key]['spec_input_type_name'] = $this->spec_input_type[$val['spec_input_type']];
			}
		}
		/*echo "<pre>";
		print_r($attributes);die();
		echo "</pre>";*/
		$this->assign('cid',  $cid);
		$this->assign('specs', $specs);
		$this->assign('specs_count', count($specs));

		/* 导入jQuery的表单验证插件 */
		$this->import_resource(array(
		'script' => 'jqtable.js,inline_edit.js',
		'style'  => 'res:style/jqtreetable.css'
		));

		$this->display('spec.index.html');
		//print_r($a);
	}
	/**
     *    新增商品品牌
     *
     *    @author    Hyber
     *    @return    void
     */
	function add()
	{
		if (!IS_POST)
		{
			$cid  =  intval($_GET['cid']);
			/* 显示新增表单 */
			$attribute = array(
			'sort_order' => 255,
			'spec_input_type' => 0,
			);
			
			
			$this->import_resource(array(
			'script' => 'jquery.plugins/jquery.validate.js'
			));
			$this->assign('cid', $cid);
			$this->assign('attr_input_type', $this->spec_input_type);
			$this->assign('spec', $spec);

			$this->display('spec.form.html');
		}
		else
		{
			$data = array();
			$data['spec_name']     = $_POST['spec_name'];
			$data['sort_order']    = $_POST['sort_order'];
			$data['spec_input_type']    = $_POST['spec_input_type'];
			$data['spec_values']    = $_POST['spec_values'];
			$data['cate_id'] = $_POST['cid'];

			/* 检查名称是否已存在 */
			if (!$this->_spec_mod->unique(trim($data['spec_name']),$data['cate_id']))
			{
				$this->show_warning('该规格名称已经存在了，请您换一个');
				return;
			}
			if (!$brand_id = $this->_spec_mod->add($data))  //获取brand_id
			{
				$this->show_warning($this->_spec_mod->get_error());

				return;
			}


			$this->show_message('添加规格成功',
			'back_list',    'index.php?app=spec&amp;cid='.$_POST['cid'],
			'continue_add', 'index.php?app=spec&amp;act=add&amp;cid='.$_POST['cid']
			);
		}
	}

	/* 检查规格唯一 */
	function check_spec_name ()
	{
		$spec_name = empty($_GET['spec_name']) ? '' : trim($_GET['spec_name']);
		$cate_id   = empty($_GET['cid']) ? 0 : intval($_GET['cid']);
		$spec_id   = empty($_GET['id']) ? 0 : intval($_GET['id']);
		if (!$spec_name) {
			echo ecm_json_encode(false);
		}
		if ($this->_spec_mod->unique($spec_name, $cate_id,$spec_id)) {
			echo ecm_json_encode(true);
		}
		else
		{
			echo ecm_json_encode(false);
		}
		return ;
	}

	/**
     *    编辑商品品牌
     *
     *    @author    Hyber
     *    @return    void
     */
	function edit()
	{
		$spec_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
		$cate_id = isset($_GET['cid']) ? intval($_GET['cid']) : 0;
		if (!$spec_id)
		{
			$this->show_warning('no_such_brand');
			return;
		}
		if (!IS_POST)
		{
			$find_data     = $this->_spec_mod->find($spec_id);
			if (empty($find_data))
			{
				$this->show_warning('no_such_brand');

				return;
			}
			$spec    =   current($find_data);

			$cid  =  intval($_GET['cid']);

			$this->import_resource(array(
			'script' => 'jquery.plugins/jquery.validate.js'
			));

			$this->assign('cid', $spec['cate_id']);
			$this->assign('spec_input_type', $this->spec_input_type);
			$this->assign('spec', $spec);

			$this->display('spec.form.html');
		}
		else
		{
			$data = array();
			$data['spec_name']     = $_POST['spec_name'];
			$data['sort_order']    = $_POST['sort_order'];
			$data['spec_input_type']    = $_POST['spec_input_type'];
			$data['spec_values']    = $_POST['spec_values'];
			$data['cate_id'] = $_POST['cid'];

			/* 检查名称是否已存在 */
			if (!$this->_spec_mod->unique(trim($data['spec_name']),$cate_id, $spec_id))
			{
				$this->show_warning('该规格名称已经存在了，请您换一个');
				return;
			}
			$rows=$this->_spec_mod->edit($spec_id, $data);
			if ($this->_spec_mod->has_error())
			{
				$this->show_warning($this->_spec_mod->get_error());

				return;
			}


			$this->show_message('编辑规格成功',
			'back_list',    'index.php?app=spec&amp;cid='.$_POST['cid'],
			'edit_again', 'index.php?app=spec&amp;act=edit&amp;id=' . $spec_id
			);
		}
	}

	//异步修改数据
	function ajax_col()
	{
		$id     = empty($_GET['id']) ? 0 : intval($_GET['id']);
		$column = empty($_GET['column']) ? '' : trim($_GET['column']);
		$value  = isset($_GET['value']) ? trim($_GET['value']) : '';
		$data   = array();

		if (in_array($column ,array('sort_order')))
		{
			$data[$column] = $value;

			if($this->_spec_mod->edit($id, $data))
			{
				echo ecm_json_encode(true);
			}
		}
		else
		{
			return ;
		}
		return ;
	}

	function drop()
	{
		$spec_ids = isset($_GET['id']) ? trim($_GET['id']) : '';
		if (!$spec_ids)
		{
			$this->show_warning('no_such_navigation');

			return;
		}
		$spec_ids=explode(',',$spec_ids);
		if (!$this->_spec_mod->drop($spec_ids))    //删除
		{
			$this->show_warning($this->_spec_mod->get_error());

			return;
		}

		$this->show_message('删除规格成功');
	}


}

?>